<%@page import="java.net.URI"%>
<%@ page contentType="text/html; charset=UTF-8" session = "true"%>
<%@include file = "cargarLogica.jsp" %>
<%@page import="Utilidades.MD5"%>
<%@page import="Servidor.DataPerfil"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>

<!DOCTYPE html>
<html lang="es">
    
<head>
	<title>TPlay Market - Iniciar Sesión</title>
	<meta name="description" content="Bienvenido a TPlay Market, tienda de Juegos On-Line"/>
	<meta name="viewport" content="width=device-width, initial-scale=1"/>
	<link rel="shortcut icon" type="image/xicon" href="favicon.ico"/>
	<link rel="stylesheet" type="text/css" href="css/tplay_style.css"/>
        <link rel="stylesheet" type="text/css" href="css/JF_S/inicioSesion.css"/>
</head>

<body>
        <jsp:include page="tplay_header.jsp"/>
        <jsp:include page="tplay_nav_visitante.jsp"/>
          
        <%
            
            if(iw != null){

        %>
        
        <%
        
            String invalido = new String();
            
            if(session.getAttribute("usuario") == null){
            
                String email = request.getParameter("TF_Email");
                String password = request.getParameter("TF_Password");
                Boolean valido = null;
                
                if((email != null) && (password != null)){

                    try{

                        MD5 encriptar = new MD5();
                        password = encriptar.md5(password);
                        
                        valido = ip.comprobarPassword(email, password);

                        if(valido){
                            
                            session = request.getSession();
                            DataPerfil dp = ip.obtenerPerfilXEmail(email, true);
                            session.setAttribute("usuario", dp);
                            response.sendRedirect("/TPlay_Market_Web");

                        }

                        else{

                            invalido = invalido.concat("Usuario Invalido!");

                        }

                    }

                    catch(Exception e){

                        invalido = invalido.concat("El usuario no existe!");
                            
                    }

                }

                else{

                    invalido = invalido.concat("Ingrese sus datos");

                }
                
            }
            
            else{
            
                if(!response.isCommitted()){
                
                    response.sendRedirect("/TPlay_Market_Web");
            
                }
                    
            }
        
        %>
        
	<section id="S_Contenido">
            <form id = "F_InicioSesion" action = "inicioSesion.jsp" method = "POST"><br>
                <label id = "LBL_Estado"><%= invalido%></label><br><br>
                <label id = "LBL_Email">Email</label><input id = "TF_Email" type = "text" name = "TF_Email" required = "required"/><br><br>
                <label id = "LBL_Password">Contraseña</label><input id = "TF_Password" type = "password" name = "TF_Password" required = "required"/><br><br>
                <button id = "BTN_InicioSesion" type = "submit">Iniciar Sesión</button>
                <a href = "registrar.jsp"><button id = "BTN_Registrar" type = "button" name = "BTN_Registrar">Registrar</button></a><br><br>
            </form>
	</section>
        
        <%
    
            }
            
            else{
            
                if(!response.isCommitted()){
                
                    response.sendRedirect(request.getServletContext().getContextPath());
                
                }
            
            }
        
        %>
                
        <jsp:include page="tplay_footer.jsp"/>
</body>
</html>